package com.subsequent.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subsequent.pojo.Pharmacies;
import com.subsequent.service.PharmaciesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/pharmacies")
public class PharmaciesController {
    @Autowired
    private PharmaciesService pharmaciesService;

    // 分页+条件查询
    @GetMapping("/list")
    public Page<Pharmacies> list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String pharmacyName,
            @RequestParam(required = false) String pharmacyType,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime
    ) {
        return pharmaciesService.pageQuery(pageNum, pageSize, pharmacyName, pharmacyType, status, startTime, endTime);
    }

    // 获取药房类型下拉（从数据库查询）
    @GetMapping("/types")
    public List<String> getTypes() {
        return pharmaciesService.getAllPharmacyTypes();
    }

    // 新增药房
    @PostMapping("/add")
    public boolean add(@RequestBody Pharmacies pharmacy) {
        return pharmaciesService.save(pharmacy);
    }

    // 编辑药房
    @PostMapping("/edit")
    public boolean edit(@RequestBody Pharmacies pharmacy) {
        return pharmaciesService.updateById(pharmacy);
    }

    // 启用/禁用药房
    @PostMapping("/status")
    public boolean changeStatus(@RequestParam Integer pharmacyId, @RequestParam Integer status) {
        return pharmaciesService.changeStatus(pharmacyId, status);
    }
}